home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2005 June
/
ccd0605.iso
/
Software
/
Shareware
/
Programare
/
nativej
/
nativej-trial.exe
/
{app}
/
examples-source
/
Service2.java
< prev
next >
Wrap
Text File
|
2003-04-09
|
5KB
|
156 lines
package examples;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* This is a sample Java program that runs as a service.
*
* It appends the current date/time to a file called "service.log" at 5 secs interval
* until it is stopped.
*
* The "stop" command is implemented using datagram. Since IP is used to signal
* termination, it does not matter whether this is done within the same or separate JVM
* space. As such, this approach will work with BOTH command line and
* NativeJ-generated executables.
*/
public class Service2
{
/**
* This is the port over which the termination signal is sent.
*/
private final static int port = 5678;
/**
* This is the message to be sent to signal termination.
*/
private final static String terminator = "QUIT";
/**
* The main() function accepts one single parameter: "start" or "stop".
* The first parameter starts the date/time logging service, while the
* second parameter stops the service.
*/
public static void main(String[] args) throws Exception
{
// Start the service
if (args[0].equals("start"))
{
try
{
// Run the termination listener thread
TerminationListener t = new TerminationListener(
port, terminator, Thread.currentThread());
t.start();
// Start the logging service
while(true)
{
// Append current date/time to log file
log("Current date/time is " + new Date());
// Sleep for 5 secs
Thread.currentThread().sleep(5000);
}
}
catch(InterruptedException e)
{
// Exit when thread is interrupted.
log("Service stopped.");
}
}
else
// Stop the service
if (args[0].equals("stop"))
{
try
{
// Send the termination message
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(
terminator.getBytes(), terminator.length(),
InetAddress.getLocalHost(), port);
socket.send(packet);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/**
* Log given string to file "service.log".
*/
static void log(String msg) throws IOException
{
PrintWriter pw = new PrintWriter(new FileWriter("service.log", true));
pw.println(msg);
pw.close();
}
}
/**
* This is a thread that will listen for the termination message over the
* designated port, then interrupt the parent thread.
*/
class TerminationListener extends Thread
{
Thread parent;
int port;
String terminator;
/**
* Set this thread to daemon mode so that if for some reason the main
* thread exists, this thread will not prevent the JVM from terminating.
*/
public TerminationListener(int port, String terminator, Thread parent)
{
setDaemon(true);
this.port = port;
this.terminator = terminator;
this.parent = parent;
}
/**
* Listen for the termination signal over the designated port.
*/
public void run()
{
try
{
// Setup datagram socket
DatagramSocket socket = new DatagramSocket(port);
DatagramPacket packet = new DatagramPacket(
new byte[terminator.length()], terminator.length());
// Stop only when we have received the termination message
while(true)
{
// Wait for a message
socket.receive(packet);
String msg = new String(packet.getData());
// Make sure the message is coming from the same machine.
// This is included for additional security so that the program
// cannot be terminated from an external machine).
if (!packet.getAddress().equals(InetAddress.getLocalHost())) continue;
// Make sure the message is the termination message
if (!msg.equals(terminator)) continue;
// Interrupt parent thread
parent.interrupt();
// Terminate this thread
break;
}
}
catch(Exception e)
{
}
}
}